<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */
declare (strict_types = 1);

namespace app\model;
use think\facade\Db;
use think\Model;

class User extends Model
{

    //在模型中定义 时间戳转化格式
     protected $autoWriteTimestamp = true;
     protected $dateFormat = 'Y-m-d H:i:s';
     protected $type = [
            'register_time' => 'timestamp',
            'last_login_time' => 'timestamp', 
            'update_time'   => 'timestamp',     
          ];
    /**
    * 获取分页列表
    * @param $where
    * @param $param
    */
    public function getUserList($where, $param)
    {
		$rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
		$order = empty($param['order']) ? 'id desc' : $param['order'];
        $list = self::where($where)->field('id,bj_code,st_code,nickname,username,password,salt,name,login_pc,mobile,email,sfz,headimgurl,sex,desc,birthday,country,province,city,school,address,depament,position,level,status,last_login_time,last_login_ip,login_num,register_time,register_ip,update_time,score,score1,score2')->order($order)->paginate($rows, false, ['query' => $param]);
		return $list;
    }
    /**
    * 获取分页列表
    * @param $where
    * @param $param
    */
    public function getClassSeatsList($where, $param)
    {
        $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
        $order = empty($param['order']) ? 'login_pc asc' : $param['order'];
        $list = self::where($where)->field('id,bj_code,st_code,nickname,username,password,salt,name,login_pc,mobile,email,sfz,headimgurl,sex,desc,birthday,country,province,city,school,address,depament,position,level,status,last_login_time,last_login_ip,login_num,register_time,register_ip,update_time,score,score1,score2')->order($order)->paginate($rows, false, ['query' => $param]);
        return $list;
    }

    /**
    * 添加数据
    * @param $param
    */
    public function addUser($param)
    {
		$insertId = 0;
        try {
			$param['create_time'] = time();
			$insertId = self::strict(false)->field(true)->insertGetId($param);
			add_log('add', $insertId, $param);
        } catch(\Exception $e) {
			return to_assign(1, '操作失败，原因：'.$e->getMessage());
        }
		return to_assign(0,'操作成功',['aid'=>$insertId]);
    }

    /**
    * 编辑信息
    * @param $param
    */
    public function editUser($param)
    {
        try {
            $param['update_time'] = time();
            self::where('id', $param['id'])->strict(false)->field(true)->update($param);
			add_log('edit', $param['id'], $param);
        } catch(\Exception $e) {
			return to_assign(1, '操作失败，原因：'.$e->getMessage());
        }
		return to_assign();
    }
	

    /**
    * 根据id获取信息
    * @param $id
    */
    public function getUserById($id)
    {
        $info = self::where('id', $id)->find();
		return $info;
    }


}

